/*
 * @lc app=leetcode.cn id=653 lang=cpp
 *
 * [653] 两数之和 IV - 输入二叉搜索树
 */

// @lc code=start
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
#include <bits/stdc++.h>
using namespace std;
class Solution
{
    TreeNode* find(TreeNode *root, const int target)
    {
        if (root)
        {
            if (root->val == target)
                return root;
            if (root->val > target)
                return find(root->left, target);
            else
                return find(root->right, target);
        }
        return nullptr;
    }

public:
    TreeNode *root;
    bool find2(TreeNode *root, int k)
    {
        if (!root)
            return false;
        auto tmp = find(this->root, k - root->val);
        if (tmp && tmp != root)
        {
            return true;
        }
        return find2(root->left, k) || find2(root->right, k);
    }
    bool findTarget(TreeNode *root, int k)
    {
        this->root = root;

        return find2(root, k);
    }
};
// @lc code=end
